<?php
/**
 * Description
 *
 * PHP Version 5.6
 *
 * @category TelegramPostBot
 * @package  Data
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     https://mit-license.org/
 * @date     2018-12-21 10:48:49
 * @modifyby ctwj
 */

namespace Telegram\Data;

use Telegram\Exception\DatabaseException;

/**
 * Message class
 *
 * 消息
 *
 * @category PHP
 * @package  Data
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 */
class BaseModel
{

    /**
     * Medoo对象
     *
     * @var [\Meddo\Medoo]
     */
    public $instance;

    /**
     * 表名
     *
     * @var [string]
     */
    public $table;

    /**
     * 构造函数
     *
     * @param [type] $c Container
     *
     * @return void
     */
    public function __construct($c)
    {
        $this->instance = $c['database'];
    }

    /**
     * 添加数据
     *
     * @param [array] $data 数据
     *
     * @return integer
     */
    public function add($data)
    {
        try {
            $data = $this->instance->insert($this->table, $data);
            $this->checkError();
            return $this->instance->id();
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * 删除数据
     *
     * @param [array] $condition 条件
     *
     * @return integer
     */
    public function remove($condition)
    {
        try {
            $this->instance->delete($this->table, $condition);
            $this->checkError();
            return $this->instance->rowCount();
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * 跟新数据
     *
     * @param [array] $data      数据
     * @param [array] $condition 条件
     *
     * @return boolean
     */
    public function update($data, $condition)
    {
        try {
            $data = $this->instance->update($this->table, $data, $condition);
            $this->checkError();
            return $data->rowCount();
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * 执行sql
     *
     * @param [string] $sql  SQL语句
     * @param [array]  $bind 数据绑定
     *
     * @return array
     */
    public function query($sql, $bind = null)
    {
        try {
            if (is_null($bind)) {
                $result = $this->instance->query($sql)->fetchAll();
            } else {
                $result = $this->instance->query($sql, $bind)->fetchAll();
            }
            $this->checkError();
            return $result;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * 获取完整表名
     *
     * @param [string] $table 表名
     *
     * @return string;
     */
    public function tableName($table)
    {
        return getenv('DB_PREFIX') . $table;
    }

    /**
     * 查找数据
     *
     * @param [string/array] $field     字段
     * @param [array]        $condition 条件
     *
     * @return array
     */
    public function find($field = '*', $condition = '1=1')
    {
        try {
            $result = $this->instance->select($this->table, $field, $condition);
            $this->checkError();
            return $result;
        } catch (\Exception $e) {
            echo 'a';
            throw $e;
        }
    }

    /**
     * 获取一条记录
     *
     * @param [type]  $condition 条件
     * @param [mixed] $field     字段
     *
     * @return array
     */
    public function get($condition, $field = '*')
    {
        try {
            $result = $this->instance->get($this->table, $field, $condition);
            $this->checkError();
            return $result;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Count
     *
     * @param [array] $condition 条件
     *
     * @return integer
     */
    public function count($condition)
    {
        try {
            $count = $this->instance->count($this->table, $condition);
            $this->checkError();
            return $count;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Has Record?
     *
     * @param [array] $condition 条件
     *
     * @return boolean
     */
    public function exists($condition)
    {
        try {
            $data = $this->instance->has($this->table, $condition);
            $this->checkError();
            return $data;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Check Sql Error, 当开启调试时，抛出异常
     *
     * @return void
     */
    public function checkError()
    {
        global $tpb_debug;
        $error = $this->instance->error();
        if (!is_null($error[1])) {
            $error['sql'] = $this->instance->last();
            getService('logger')->error('Sql Query Error.', $error);
            if ($tpb_debug) {
                throw new \Telegram\Exception\DatabaseException($error[2], $error[1]);
            }
        }
    }
}
